// 引入静态路由
import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  //routes是路由表,默认有静态路由的权限
  routes: constantRoutes
}
const mutations = {
  //修改state里面的routes
  setRoutes (state, newRoutes) {
    // newRoutes 是传送过来拥有的路由权限
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  //筛选动态路由的方法
  //menus是传过来的拥有的路由数组
  filterRoutes (context, menus) {
    let routes = []
    menus.map(key => {
      routes.push(...asyncRoutes.filter(item => item.name == key))
    })
    // menus.forEach(key => {
    //   routes.push(...asyncRoutes.filter(item => item.name == key))
    // })
    //调用方法更新state的routes  // 将动态路由提交给mutations
    context.commit('setRoutes', routes)
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}